<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use app\common\model\UserBookError;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class Smoke extends AdminBase
{
    /**\
     * 品牌库
     */
    public function brand_index()
    {
        $name = input('param.name', '', 'trim');
        $category_id = input('param.category_id', '', 'trim');
        $where = [];
        if (!empty($name)) {
            $where[] = [
                'name', 'like', '%' . $name . '%'
            ];
        }
        if (!empty($category_id)) {
            $where[] = [
                'category_id', '=', $category_id
            ];
        }
        $model = Db::name('smoke_brand')
            ->where($where)
            ->order(['hot' => 'desc'])
            ->column('id,name,hot,category_id');
        $count=Db::name('smoke_brand')
            ->where($where)
            ->count();
        $category=Db::name('smoke_category')
            ->column('id,name');
        return View::fetch("brand_index", ['model' => $model, 'name' => $name,'count' => $count,'category_id'=>$category_id,'category'=>$category]);
    }
    /**\
     * 品牌新增和修改
     */
    public function brand_edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            //判断是否为修改
            //修改就一步
            Db::startTrans();
            try {
                $data['update_time'] = time();
                if ($data['id']){
                    //修改
                    Db::name('smoke_brand')
                        ->where('id', $data['id'])
                        ->update($data);
                }else{
                    $data['create_time'] = time();
                    Db::name('smoke_brand')
                        ->insert($data);
                }
                Db::commit();
                return show(1, '保存成功');
            } catch (\Exception $e) {
                Db::rollback();
                return show(0, $e->getMessage());
            }

        } else {
            $id = input('id');
            $model = Db::name('smoke_brand')
                ->where('id', $id)
                ->find();
            $category=Db::name('smoke_category')
                ->column('id,name');
            return View::fetch('brand_edit', ['model' => $model,'category'=>$category]);
        }
    }
    /**\
     * 香烟库
     */
    public function index()
    {
        $name = input('param.name', '', 'trim');
        $category_id = input('param.category_id', '', 'trim');
        $where = [];
        if (!empty($name)) {
            $where[] = [
                'name', 'like', '%' . $name . '%'
            ];
        }
        if (!empty($category_id)) {
            $where[] = [
                'category_id', '=', $category_id
            ];
        }
        $model = Db::name('smoke')
            ->where($where)
            ->order(['hot' => 'desc'])
            ->column('id,name,hot,price,img,brand_id');
        $count=Db::name('smoke')
            ->where($where)
            ->count();
        $category=Db::name('smoke_category')
            ->column('id,name');
        return View::fetch("index", ['model' => $model, 'name' => $name,'count' => $count,'category_id'=>$category_id,'category'=>$category]);
    }
    /**\
     * 香烟新增和修改
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            //判断是否为修改
            //修改就一步
            Db::startTrans();
            try {
                $data['update_time'] = time();
                if ($data['id']){
                    //修改
                    Db::name('smoke')
                        ->where('id', $data['id'])
                        ->update($data);
                }else{
                    $data['create_time'] = time();
                    Db::name('smoke')
                        ->insert($data);
                }
                Db::commit();
                return show(1, '保存成功');
            } catch (\Exception $e) {
                Db::rollback();
                return show(0, $e->getMessage());
            }

        } else {
            $id = input('id');
            $model = Db::name('smoke')
                ->where('id', $id)
                ->find();
            $category=Db::name('smoke_category')
                ->column('id,name');
            $brand=Db::name('smoke_brand')
                ->where('category_id',1)
                ->column('id,name');
            return View::fetch('edit', ['model' => $model,'category'=>$category,'brand'=>$brand]);
        }
    }
    public function get_brand(){
        $category_id=input('param.category_id');
        $brand=Db::name('smoke_brand')
            ->where('category_id',$category_id)
            ->order('hot','desc')
            ->column('id,name');
        return show(1,'',$brand);
    }
    /**\
     * 香烟库
     */
    public function chongfu_index()
    {
        $name = input('param.name', '', 'trim');
        $category_id = input('param.category_id', '', 'trim');
        $where = [];
        if (!empty($name)) {
            $where[] = [
                'name', 'like', '%' . $name . '%'
            ];
        }
        if (!empty($category_id)) {
            $where[] = [
                'category_id', '=', $category_id
            ];
        }
        $sql="SELECT name, COUNT(id) AS user_count
FROM bingdi_smoke
WHERE category_id = 1
GROUP BY name
HAVING COUNT(id) > 1
ORDER BY user_count DESC";
        $model=Db::query($sql);
        return View::fetch("chongfu_index", ['model' => $model, 'name' => $name,'count'=>count($model)]);
        dd($model);
        $model = Db::name('smoke')
            ->where($where)
            ->order(['hot' => 'desc'])
            ->column('id,name,hot,price,img,brand_id');
        $count=Db::name('smoke')
            ->where($where)
            ->count();
        $category=Db::name('smoke_category')
            ->column('id,name');
        return View::fetch("chongfu_index", ['model' => $model, 'name' => $name,'count' => $count,'category_id'=>$category_id,'category'=>$category]);
    }
    public function chongfu_detail()
    {
        $name=input('name');
        $model = Db::name('smoke')
            ->alias('s')
            ->leftJoin('smoke_brand b','b.id=s.brand_id')
            ->where('s.name', $name)
            ->column('s.id,s.name,s.price,s.img,b.name as brand_name,s.carton_code');
        $ids=array_column($model,'id');
        unset($ids[0]);
        $id=implode(",",$ids);
        $sql="DELETE FROM bingdi_smoke WHERE id IN (".$id.");";
        return View::fetch("chongfu_detail", ['model' => $model, 'name' => $sql,'count' => count($model)]);
    }
    public function chongfu_delete(){
        $sql=input("name");
        $sql=Db::query($sql);
        dd($sql);
    }
}